/**
 * @version 2007-1-25
 * @author xalinx at gmail dot com
 */
package org.nanhill.commons.io;

import java.io.File;
import java.io.IOException;

/**
 * @version 2007-1-25
 * @author xalinx at gmail dot com
 * 
 */
public abstract class AbstractFileConverter implements FileConverter {

	/**
	 * @see org.nanhill.commons.io.FileConverter#convert(java.io.File,
	 *      java.io.File)
	 */
	public void convert(File fromFile, File toFile) throws IOException {
		if (this.isBreakVisit()) {
			return;
		}

		if (fromFile.isDirectory()) {
			this.convertDir(fromFile, toFile);
			// { visit child files
			String[] children = fromFile.list();
			for (int i = 0; i < children.length; i++) {
				convert(new File(fromFile, children[i]), new File(toFile,
						children[i]));
			}
			// }
		} else {
			this.convertFile(fromFile, toFile);
		}
	}

	/**
	 * Check whether the visiting need be breaked.
	 * 
	 * @return true if need be breaked, else false.
	 */
	protected boolean isBreakVisit() {
		return false;
	}

	/**
	 * File visit hook.
	 * 
	 * @param file
	 */
	protected abstract void convertFile(File fromFile, File toFile)
			throws IOException;

	/**
	 * Dir visit hook.
	 * 
	 * @param dir
	 */
	protected abstract void convertDir(File fromDir, File toDir)
			throws IOException;

}
